#pragma once

#include <iostream>
#include <thread>
#include <vector>
#include <mutex>
#include <easylog.h>
#include "util.hh"

namespace cloud
{

#define CONFIG_FILE "cloud.json"

struct config
{
    static config& instance()
    {
        static config inst;
        return inst;
    }

    const int                       gap_time()      const { return _gap_time;      }
    const std::string&              upload_prefix() const { return _upload_prefix; }
    const std::vector<std::string>& back_dirs()     const { return _back_dirs;     }
    const std::string&              record_file()   const { return _record_file;   }
    const std::string&              server_ip()     const { return _server_ip;     }
    const int                       server_port()   const { return _server_port;   }

private:
    config() : _config_file(CONFIG_FILE)
    {
        util::file conf(_config_file);

        if (!conf.exists())
            throw except("config file is not exists");

        easylog::info("config instance created");

        util::json js;
        js.deserialize(conf.read());

        _gap_time      = js.get<int>("gap_time");
        _upload_prefix = js.get<std::string>("upload_prefix");
        _back_dirs     = js.get<std::vector<std::string>>("back_dirs");
        _record_file   = js.get<std::string>("record_file");
        _server_ip     = js.get<std::string>("server_ip");
        _server_port   = js.get<int>("server_port");
    }

private:
                      size_t _gap_time;
                 std::string _upload_prefix;
    std::vector<std::string> _back_dirs;
                 std::string _record_file;
                 std::string _server_ip;
                    uint16_t _server_port;

                 std::string _config_file;
};
}
